<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>sort</title>
  <script type="text/javascript">
  <!--
	window.alert=function(s){ console.log(s);};
	var a;
	// 看上去正常的结果:
	a = ['Google', 'Apple', 'Microsoft'].sort(); // ['Apple', 'Google', 'Microsoft'];
	alert(a);

	// apple排在了最后:
	a = ['Google', 'apple', 'Microsoft'].sort(); // ['Google', 'Microsoft", 'apple']
	alert(a);

	// 无法理解的结果:
	a = [10, 20, 1, 2].sort(); // [1, 10, 2, 20]
	alert(a);

	var arr = [10, 20, 1, 2];
	a = arr.sort(function (x, y) {
		if (x < y) {
			return -1;
		}
		if (x > y) {
			return 1;
		}
		return 0;
	}); // [1, 2, 10, 20]
	alert("arr:"+arr);
	alert(a);

	arr = [10, 20, 1, 2];
	a = arr.sort(function (x, y) {
		if (x < y) {
			return 1;
		}
		if (x > y) {
			return -1;
		}
		return 0;
	}); // [20, 10, 2, 1]
	alert("arr:"+arr);
	alert(a);

	arr = ['Google', 'apple', 'Microsoft'];
	arr.sort(function (s1, s2) {
		x1 = s1.toUpperCase();
		x2 = s2.toUpperCase();
		if (x1 < x2) {
			return -1;
		}
		if (x1 > x2) {
			return 1;
		}
		return 0;
	}); // ['apple', 'Google', 'Microsoft']
	alert("arr:"+arr);

	var a1 = ['B', 'A', 'C'];
	var a2 = a1.sort();
	alert(a1); // ['A', 'B', 'C']
	alert(a2); // ['A', 'B', 'C']
	alert(a1 === a2); // true, a1和a2是同一对象

  //-->
  </script>
 </head>
 <body>
  
 </body>
</html>
